package cl.gob.minvu.catastro.util;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

/**
 * @author psep
 *
 */
public final class ManagedBeanUtil {
	
	/**
	 * @param page
	 * @return
	 */
	public static final String go(String page) {
		if (page == null) {
			return null;
		} else {
			destroyManagedBean("usuarioBean");
			destroyManagedBean("fichaBean");
			destroyManagedBean("encuestadorBean");
			
			return page.trim() + "?faces-redirect=true";
		}		
	}
	
	/**
	 * @param msg
	 */
	public static final void getMessageINFO(String msg) {
		getMessage(FacesMessage.SEVERITY_INFO, "INFORMACIÓN: ", msg);
	}
	
	/**
	 * @param msg
	 */
	public static final void getMessageWARN(String msg) {
		getMessage(FacesMessage.SEVERITY_WARN, "ADVERTENCIA: ", msg);
	}
	
	/**
	 * @param msg
	 */
	public static final void getMessageERROR(String msg) {
		getMessage(FacesMessage.SEVERITY_ERROR, "ERROR: ", msg);
	}
	
	/**
	 * @param severity
	 * @param title
	 * @param msg
	 */
	public static final void getMessage(Severity severity, String title, String msg) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, msg));
	}
	
	/**
	 * @param managedBean
	 */
	public static final void destroyManagedBean(String managedBean) {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(managedBean, null);
	}

}
